#include <stdlib.h>
#include <string.h>
#include <svc.h>
#include <svctxo.h>

//Include file for ACT2000
#include <applidl.h>
#include <acldev.h>
#include <aclstr.h>
#include <message.h>
#include <printer.h>

//Include file for EESL
//#include "C:\Verixaps\VMAC\include\logsys.h"
//#include "C:\Verixaps\VMAC\include\varrec.h"
//#include "C:\Verixaps\VMAC\include\eeslapi.h"
//#include "C:\VerixVaps\VMAC\ver150\include\logsys.h"
//#include "C:\VerixVaps\VMAC\ver150\include\varrec.h"
//#include "C:\VerixVaps\VMAC\ver150\include\eeslapi.h"
#include <logsys.h>
#include <varrec.h>
#include <eeslapi.h>

//Include file for Device Manager
//#include "C:\Verixaps\VMAC\include\devman.h"
//#include "C:\VerixVaps\VMAC\ver150\include\devman.h"
#include <devman.h>
#include <battery.h>
#include <power.h>

//Application specific Include files
#include "display.txo"
#include "UserIf.h"
#include "uclcode.h"

char Iklan[1000]; //untuk iklan	
extern short hMagReader;
char szKeyMap[MAX_ALPNUM_KEYS][CHAR_PER_KEY]= {"0- +%",
"1QZ.\\", "2ABC&", "3DEF%", "4GHI*", "5JKL/", "6MNO~",
"7PRS^", "8TUV[", "9WXY]", "*,'\":", "#=;$?" };

void DisplayTopLogo()
{
	char hrf[20];
	char buff[30];
	static unsigned int i = 0;
	i++;
	buff[0] = 0x00; buff[1] = 0x01; buff[2] = 0x02; buff[3] = 0x03;
	buff[4] = 0x04; buff[5] = 0x05; buff[6] = 0x06; buff[7] = 0x07;
	get_font(hrf);

	switch (i%7){
	  case 1:
		  GetTopDateTime(buff);
		  set_font("F:asc4x16i.vft");
		  write_at(buff, 16, 1, 1);
		  break;
	  case 2:
		  GetTopDateTime(buff);
		  set_font("F:asc4x16i.vft");
		  write_at(buff, 16, 1, 1);
		  break;
	  case 3:
		  set_font("F:TopLogo1.vft");		
		  write_at(buff, 8, 1, 1);
		  break;
	  case 4:
		  GetTopDateTime(buff);
		  set_font("F:asc4x16i.vft");
		  write_at(buff, 16, 1, 1);
		  break;
	  case 5:
		  GetTopDateTime(buff);
		  set_font("F:asc4x16i.vft");
		  write_at(buff, 16, 1, 1);
		  break;
	  case 6:
		  set_font("F:TopLogo2.vft");
		  write_at(buff, 8, 1, 1);
		  break;
	}
	set_font(hrf);
/*
	char buff[] = "BANK MANDIRI: Bank Terpercaya Pilihan Anda. ";
	char hrf[20];
	char buff2[20];
	static int posisi = 0;

	strncpy(buff2, &buff[posisi++], 18); 
	if (strlen(buff2)<18) {
		strncat(buff2, buff, 18-strlen(buff2)); 
	}
	if (posisi==strlen(buff)) posisi=0;
	get_font(hrf);
	set_font("");
	write_at(buff2, 18, 4, 1);
	write_at("------------------", 18, 4, 2);
	set_font("F:bri.vft");
	buff[0] = 0; 
	write_at(buff, 1, 1, 1);
	set_font(hrf);
*/
}

void DisplaySearchingLogo()
{
	char hrf[20];
	char buff[30];
	static unsigned int i = 0;
	i++;
	buff[0] = 0x00; buff[1] = 0x01; buff[2] = 0x02; buff[3] = 0x03;
	buff[4] = 0x04; buff[5] = 0x05; buff[6] = 0x06; buff[7] = 0x07;
	get_font(hrf);

	set_font("F:TopLogo3.vft");		
	write_at(buff, 8, 1, 1);

	set_font(hrf);
}

void DisplayStatus(char *msg)
{
	int inMsgLen = 0;
	char buff[128];

	inMsgLen = strlen(msg);
	if (inMsgLen>21) inMsgLen = 21;

	if (inMsgLen==0) { display_at( 1, 8,"", CLR_EOL); return;}

	memset(buff, 0x00, sizeof(buff));
	memset(buff, 0x20, 21);
	//strncpy(&buff[(21-strlen(msg))/2], msg, strlen(msg));
	strncpy(buff, msg, inMsgLen);
	set_font("");
	//set_font("F:asc8x21i.vft");
	//set_font("F:asc8x21.vft");
	//write_at("___________________________", 21, 1, 7);
	write_at(buff, strlen(buff), 1, 8);
	//set_font("");
}

void ClearStatusLine(void)
{
	set_font("");
	display_at( 1, 8,"", CLR_EOL);
}

void DisplayJudul(char *msg)
{
	char buff[30];
	int inMsgLen = strlen(msg);

	if (inMsgLen>21) inMsgLen = 21;

	memset(buff, 0x00, sizeof(buff));
	memset(buff, 0x20, 21);
	strncpy(&buff[(21-strlen(msg))/2], msg, inMsgLen);
	set_font("F:asc8x21i.vft");
	write_at(buff, strlen(buff), 1, 1);
	set_font("");
}

void DisplayStatusFromFile(int Index)
{
	char buff[30];
	char msg[50];

	memset(msg, 0x00, sizeof(msg));
	msg_get(Index, msg);

	if (strlen(msg)>21) msg[21] = 0x00;
	memset(buff, 0x00, sizeof(buff));
	memset(buff, 0x20, 21);
	strncpy(&buff[(21-strlen(msg))/2], msg, strlen(msg));
	set_font("F:asc8x21i.vft");
	write_at(buff, strlen(buff), 1, 8);
	set_font("");
}

int DisplayIklan() 
{
	static int line=2;
	static int CurPos=0;
	int i, Mark=0;
	char buff[22];

	if (CurPos<0) {
		if (line<=8) {
			gotoxy(1, 2);
			delline();
			line++;
		} else if (CurPos==-1){
			CurPos=0;
			line=2;
			return 0;
		} else {
			CurPos++;
		}
		return 1;
	}

	if (!strlen(Iklan)) {
		strcpy(Iklan, "Bank_Mandiri: Bank_Terpercaya_Pilihan_Anda. Dapatkan Point Tambahan Bagi Nasabah Yang Melakukan Transaksi Melalui Mini ATM. Berlaku Sampai Dengan April 2004");
	}
	gotoxy(1, 2);
	delline();
	for (i=CurPos;; i++) {
		if ((Iklan[i]==0x00) && (i-CurPos<22)) {
			memset(buff, 0x00, sizeof(buff));
			if (Iklan[CurPos]==' ') {
				strcpy(buff, &Iklan[CurPos+1]);
			} else {
				strcpy(buff, &Iklan[CurPos]);
			}
			CurPos=-8;
			break;
		}
		if ((Iklan[i]==' ' || Iklan[i]=='_') && (i-CurPos<22)) {
			Mark = i;
		}
		if ((Iklan[i]==' ' || Iklan[i]=='_' || Iklan[i]==0x00) && (i-CurPos>=22)) {
			memset(buff, 0x00, sizeof(buff));
			if (Mark==0 || (i-Mark>=22)) Mark=CurPos+21;
			if (Iklan[CurPos]==' ') {
				strncpy(buff, &Iklan[CurPos+1], Mark-CurPos);
			} else {
				strncpy(buff, &Iklan[CurPos], Mark-CurPos);
			}
			CurPos = Mark;
			break;
		}
	}
	for (i=0; buff[i]!=0x00; i++) {
		if (buff[i] == '_') {
			buff[i] = ' ';
		}
	}
	write_at(buff, strlen(buff), (22-strlen(buff))/2, 8);
	line++;
	return 1;
}

int inDisclaimer(char *szDisclaimer)
{
	char buff[22];
	int i, Mark, CurPos=0;
	int line=2;

	ClearSevenLine();
	for (i=CurPos; ; i++) {
		if ((szDisclaimer[i]==0x00) && (i-CurPos<22)) {
			memset(buff, 0x00, sizeof(buff));
			strcpy(buff, &szDisclaimer[CurPos]);
			write_at(buff, strlen(buff), (23-strlen(buff))/2, line++);
			break;
		}
		if ((szDisclaimer[i]==' ') && (i-CurPos<22)) {
			Mark = i;
		}
		if ((szDisclaimer[i]==' ' || szDisclaimer[i]==0x00) && (i-CurPos>=22)) {
			memset(buff, 0x00, sizeof(buff));
			if (Mark==0 || (i-Mark>=22)) Mark=CurPos+21;
			strncpy(buff, &szDisclaimer[CurPos], Mark-CurPos);
			if (szDisclaimer[Mark] == ' ') {CurPos = Mark+1;} else {CurPos = Mark;}
			i = Mark-1;
			write_at(buff, strlen(buff), (23-strlen(buff))/2, line++);
		}
	}
	if (inHorizontalMenuChoice("", "SETUJU", "", "", "BATAL")==1) {
		return 1;
	} else {
		return 0;
	}
	return 0;
}

void LOG_PRINT_HEX(unsigned char* buf, int size)
{
	int i;
	char temp[1028*3 + 1];
	char cs[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	
	memset(temp, 0x00, sizeof(temp));
	if ( size>sizeof(temp)/3 ) size = sizeof(temp)/3;
	
	for ( i=0; i<size; i++)
	{
		temp[i*3] 	= cs[ buf[i]>>4 & 0x0F];
		temp[i*3+1] = cs[ buf[i] & 0x0F];
		temp[i*3+2]	= ' ';
	}

	for ( i=0; i<size/10+1; i++)
	{	
		char tt[31];
		memset(tt, '\0', sizeof(tt));
		memcpy(tt, &(temp[i*30]), 30);
		tt[30] = '\0';
		LOG_PRINTFF((0x08L, "(%-2d)->(%s)", i, tt));
	}
}

int get_env_x(char *param, int n, char *buff)
{
	char buffer[128];
	char bufftmp[128];
	int i, j=0;
	int ni=0;
	int inRetVal = 0;
	memset(buffer, 0x00, sizeof(buffer));

	inRetVal = get_env(param, buffer, sizeof(buffer)-1);
	if (inRetVal>0) {
		//LOG_PRINTFF((0x08L, "get_env_x (%s,%d: %d %s)", param, n, j, buffer));
		for (i=0, j=0; (buffer[i]!=0x00); i++) {
			if (buffer[i] == ';') {
				ni++;
				if (ni==n+1) {
					buffer[i] = 0x00;
					break;
				} else {
					j=i+1;
				}
			}
		}
	}
	memset(bufftmp, 0x00, sizeof(bufftmp));
	strcpy(bufftmp, &buffer[j]);
	vdTrimSpace(bufftmp);
	strcpy(buff, bufftmp);
	LOG_PRINTFF((0x08L, "get_env_x (%s,%d: %d %s)", param, n, j, buff));
	return (inRetVal);
}

int put_env_x(char *param, int n, char *buff)
{
	char buffer[128]; char OrgBuff[128];
	int i, j=0;
	int ni=0;
	memset(buffer, 0x00, sizeof(buffer));
	memset(OrgBuff, 0x00, sizeof(OrgBuff));
	if (get_env(param, buffer, sizeof(buffer)-1)>0) {
		//LOG_PRINTFF((0x08L, "get_env_x (%s,%d: %d %s)", param, n, j, buffer));
		strcpy(OrgBuff, buffer);
		for (i=0, j=0; (buffer[i]!=0x00); i++) {
			if (buffer[i] == ';') {
				ni++;
				if (ni==n+1) {
					buffer[i] = 0x00;
					break;
				} else {
					j=i+1;
				}
			}
		}
	}
    memset(buffer, 0x00, sizeof(buffer));
	strncpy(buffer, OrgBuff, j);
	strcat(buffer, buff);
	strcat(buffer, &OrgBuff[i]);
	return put_env(param, buffer, strlen(buffer));
	LOG_PRINTFF((0x08L, "put_env_x :%s", buffer));
}

int YesNoChoice(char *QLine3, char *QLine4, int Default)
{
	int i, karakter=0;
	vdFlushKeyboardBuffer();
	if ((strlen(QLine3)) || (strlen(QLine4))) {
		ClearSevenLine();
		if (strlen(QLine3)) write_at(QLine3, strlen(QLine3), 1, 3);
		if (strlen(QLine4)) write_at(QLine4, strlen(QLine4), 1, 4);
	}
	write_at(" Yes"			, 4, 22-4, 6);
	write_at("  No"			, 4, 22-4, 8);
	set_font("F:asc8x21.vft");
	for(i=2; i<=8; i++) write_at("|", 1, 22-5, i);
	set_font("");
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return Default;
			SVC_WAIT(0);
			continue;
		}
		if (karakter == 0x0D || karakter == 0x03) { //enter or F3
			return 1;
		} else if (karakter == 0x1B || karakter == 0x04) { //esc or F4
			return 0;
		} 
	} while(1);
}

int inBGMenuChoice(char *Label, char *MenuItem1, char *MenuItem2, char *MenuItem3, char *MenuItem4, int inBackGround)
{
	int i, karakter=0;
	int inMaxStrLen = 0;
	vdFlushKeyboardBuffer();
	ClearSevenLine();

	if (strlen(MenuItem1)>15)	MenuItem1[15] = 0x00;
	if (strlen(MenuItem2)>15)	MenuItem2[15] = 0x00;
	if (strlen(MenuItem3)>15)	MenuItem3[15] = 0x00;
	if (strlen(MenuItem4)>15)	MenuItem4[15] = 0x00;
	if (strlen(Label)>15) Label[15] = 0x00;
	if (strlen(MenuItem1)>inMaxStrLen) inMaxStrLen = strlen(MenuItem1);
	if (strlen(MenuItem2)>inMaxStrLen) inMaxStrLen = strlen(MenuItem2);
	if (strlen(MenuItem3)>inMaxStrLen) inMaxStrLen = strlen(MenuItem3);
	if (strlen(MenuItem4)>inMaxStrLen) inMaxStrLen = strlen(MenuItem4);
	if (strlen(Label)+inMaxStrLen > 20)	Label[20-inMaxStrLen] = 0x00;

	set_font("F:asc8x21.vft");
	for(i=2; i<=8; i++) write_at("|", 1, 21-inMaxStrLen, i);
	display_at(1,7, Label, NO_CLEAR);
	display_at(22-strlen(MenuItem1), 2, MenuItem1, NO_CLEAR);
	display_at(22-strlen(MenuItem2), 4, MenuItem2, NO_CLEAR);
	display_at(22-strlen(MenuItem3), 6, MenuItem3, NO_CLEAR);
	display_at(22-strlen(MenuItem4), 8, MenuItem4, NO_CLEAR);
	set_font("");

	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			if (inBackGround) inBackGroundWirelessControl(0, MODEM_PPP_CONNECTED);
			SVC_WAIT(0);
			continue;
		}
		if (karakter == 0x01 && strlen(MenuItem1)) {        //F1
			return 1;
		} else if (karakter == 0x02 && strlen(MenuItem2)) { //F2
			return 2;
		} else if (karakter == 0x03 && strlen(MenuItem3)) { //F3
			return 3;
		} else if (karakter == 0x04 && strlen(MenuItem4)) { //F4
			return 4;
		} else if (karakter == 0x1B) { //esc, no choice
			return 0;
		}
	} while(1);
	return 0;
}

int inMenuChoice(char *Label, char *MenuItem1, char *MenuItem2, char *MenuItem3, char *MenuItem4)
{
	int i, karakter=0;
	int inMaxStrLen = 0;
	vdFlushKeyboardBuffer();
	ClearSevenLine();

	if (strlen(MenuItem1)>15)	MenuItem1[15] = 0x00;
	if (strlen(MenuItem2)>15)	MenuItem2[15] = 0x00;
	if (strlen(MenuItem3)>15)	MenuItem3[15] = 0x00;
	if (strlen(MenuItem4)>15)	MenuItem4[15] = 0x00;
	if (strlen(Label)>15) Label[15] = 0x00;
	if (strlen(MenuItem1)>inMaxStrLen) inMaxStrLen = strlen(MenuItem1);
	if (strlen(MenuItem2)>inMaxStrLen) inMaxStrLen = strlen(MenuItem2);
	if (strlen(MenuItem3)>inMaxStrLen) inMaxStrLen = strlen(MenuItem3);
	if (strlen(MenuItem4)>inMaxStrLen) inMaxStrLen = strlen(MenuItem4);
	if (strlen(Label)+inMaxStrLen > 20)	Label[20-inMaxStrLen] = 0x00;

	set_font("F:asc8x21.vft");
	for(i=2; i<=8; i++) write_at("|", 1, 21-inMaxStrLen, i);
	display_at(1,7, Label, NO_CLEAR);
	display_at(22-strlen(MenuItem1), 2, MenuItem1, NO_CLEAR);
	display_at(22-strlen(MenuItem2), 4, MenuItem2, NO_CLEAR);
	display_at(22-strlen(MenuItem3), 6, MenuItem3, NO_CLEAR);
	display_at(22-strlen(MenuItem4), 8, MenuItem4, NO_CLEAR);
	set_font("");

	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if (karakter == 0x01 && strlen(MenuItem1)) {        //F1
			return 1;
		} else if (karakter == 0x02 && strlen(MenuItem2)) { //F2
			return 2;
		} else if (karakter == 0x03 && strlen(MenuItem3)) { //F3
			return 3;
		} else if (karakter == 0x04 && strlen(MenuItem4)) { //F4
			return 4;
		} else if (karakter == 0x1B) { //esc, no choice
			return 0;
		}
	} while(1);
	return 0;
}

int inHorizontalMenuChoice(char *Label, char *MenuItem1, char *MenuItem2, char *MenuItem3, char *MenuItem4)
{
	int i, karakter=0;
	int inMaxStrLen = 0;
	vdFlushKeyboardBuffer();
	ClearStatusLine();

	if (strlen(MenuItem1)>6)	MenuItem1[6] = 0x00;
	if (strlen(MenuItem2)>6)	MenuItem2[6] = 0x00;
	if (strlen(MenuItem3)>6)	MenuItem3[6] = 0x00;
	if (strlen(MenuItem4)>6)	MenuItem4[6] = 0x00;
	if (strlen(Label)>21) Label[21] = 0x00;

	set_font("");
	if (strlen(Label)) display_at(1,7, Label, CLR_EOL);
	set_font("F:asc8x21i.vft");
	if (strlen(MenuItem1)) display_at(1, 8, MenuItem1, NO_CLEAR);
	if (strlen(MenuItem2)) display_at(6, 8, MenuItem2, NO_CLEAR);
	if (strlen(MenuItem3) && !strlen(MenuItem4)) display_at(12, 8, MenuItem3, NO_CLEAR);
	if (strlen(MenuItem4)) {
		if (strlen(MenuItem3)){
			display_at(22-strlen(MenuItem4)-strlen(MenuItem3)-1, 8, MenuItem3, NO_CLEAR);
		}
		display_at(22-strlen(MenuItem4), 8, MenuItem4, NO_CLEAR);
	}

	set_font("");
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if (karakter == 97 && strlen(MenuItem1)) {        //key_a
			if (strlen(Label)) display_at(22-strlen(MenuItem1), 7, MenuItem1, NO_CLEAR);
			return 1;
		} else if (karakter == 98 && strlen(MenuItem2)) { //key_b
			if (strlen(Label)) display_at(22-strlen(MenuItem2), 7, MenuItem2, NO_CLEAR);
			return 2;
		} else if (karakter == 99 && strlen(MenuItem3)) { //key_c
			if (strlen(Label)) display_at(22-strlen(MenuItem3), 7, MenuItem3, NO_CLEAR);
			return 3;
		} else if (karakter == 100 && strlen(MenuItem4)) { //key_d
			if (strlen(Label)) display_at(22-strlen(MenuItem4), 7, MenuItem4, NO_CLEAR);
			return 4;
		} else if (karakter == 0x1B) { //esc, no choice
			return 0;
		}
	} while(1);
	return 0;
}

//return=0 => cancel
//return=1 => Number entered
//mode==0 => input as string
//mode==1 => input as formated number
//mode==2 => input as unformated number
int InputNumberInd(char *label, char *Number, long Min, long Max, int mode, long *ret)
{
	int karakter=0;
	long Num;
	char buff[40];
	int inAddLine = 0;
	char szLabel[30];

	memset(szLabel, 0x00, sizeof(szLabel));
	strcpy(szLabel, label);
	if (strlen(szLabel)>1 && szLabel[strlen(szLabel)-1] == '-') {
		inAddLine = 1;
		szLabel[strlen(szLabel)-1] = 0x00;
	}

	set_font("");
	display_at(1, 7, szLabel, CLR_EOL);
	if (inAddLine) vdScrollDisplay();
	write_at("0", 1, 21, 7);
	vdFlushKeyboardBuffer();
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if (karakter >= 0x30 && karakter <= 0x39) {
			if (strlen(Number)<21 && ((mode!=3) || (strlen(Number)<Max))) {
				Number[strlen(Number)] = karakter;
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberInd(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==8) {
			if (strlen(Number)>0) {
				Number[strlen(Number)-1] = 0x00;	
				if (inAddLine) {
					display_at(1, 6, szLabel, CLR_EOL);
					display_at(1, 7, " ", CLR_EOL);
				} else {
					display_at(1, 7, szLabel, CLR_EOL);
				}
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberInd(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==13){
			Num = atol(Number);
			if (((mode==1 || mode==2) && (Num < Min || Num > Max)) 
				|| (mode==3 && strlen(Number)<Min)){
				memset(Number, 0x00, strlen(Number));
				DisplayStatus("Out of range...!");
				vdWarnBeep();
				SVC_WAIT(1000);
				write_at("                     ", 21, 1, 7);
				if (inAddLine) {
					write_at(szLabel, strlen(szLabel), 1, 6);
				} else {
					write_at(szLabel, strlen(szLabel), 1, 7);
				}
				write_at("0", 1, 21, 7);
				DisplayStatus("");
			} else {
				*ret = Num;
				return 1;
			}
		} else if (karakter==27){
			return 0;
		} 
	} while (1);
	return 1;
}


int BGInputNumberInd(char *label, char *Number, long Min, long Max, int mode, long *ret, int inBackGround)
{
	int karakter=0;
	long Num;
	char buff[40];
	int inAddLine = 0;
	char szLabel[30];

	memset(szLabel, 0x00, sizeof(szLabel));
	strcpy(szLabel, label);
	if (strlen(szLabel)>1 && szLabel[strlen(szLabel)-1] == '-') {
		inAddLine = 1;
		szLabel[strlen(szLabel)-1] = 0x00;
	}

	set_font("");
	display_at(1, 7, szLabel, CLR_EOL);
	if (inAddLine) vdScrollDisplay();
	write_at("0", 1, 21, 7);
	vdFlushKeyboardBuffer();
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			if (inBackGround) inBackGroundWirelessControl(0, MODEM_PPP_CONNECTED);
			SVC_WAIT(0);
			continue;
		}
		if (karakter >= 0x30 && karakter <= 0x39) {
			if (strlen(Number)<21 && ((mode!=3) || (strlen(Number)<Max))) {
				Number[strlen(Number)] = karakter;
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberInd(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==8) {
			if (strlen(Number)>0) {
				Number[strlen(Number)-1] = 0x00;	
				if (inAddLine) {
					display_at(1, 6, szLabel, CLR_EOL);
					display_at(1, 7, " ", CLR_EOL);
				} else {
					display_at(1, 7, szLabel, CLR_EOL);
				}
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberInd(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==13){
			Num = atol(Number);
			if (((mode==1 || mode==2) && (Num < Min || Num > Max)) 
				|| (mode==3 && strlen(Number)<Min)){
				memset(Number, 0x00, strlen(Number));
				DisplayStatus("Out of range...!");
				vdWarnBeep();
				SVC_WAIT(1000);
				write_at("                     ", 21, 1, 7);
				if (inAddLine) {
					write_at(szLabel, strlen(szLabel), 1, 6);
				} else {
					write_at(szLabel, strlen(szLabel), 1, 7);
				}
				write_at("0", 1, 21, 7);
				DisplayStatus("");
			} else {
				*ret = Num;
				return 1;
			}
		} else if (karakter==27){
			return 0;
		} 
	} while (1);
	return 1;
}

int InputNumberEngl(char *label, char *Number, long Min, long Max, int mode, long *ret)
{
	int karakter=0;
	long Num;
	char buff[40];
	int inAddLine = 0;
	char szLabel[30];

	memset(szLabel, 0x00, sizeof(szLabel));
	strcpy(szLabel, label);
	if (strlen(szLabel)>1 && szLabel[strlen(szLabel)-1] == '-') {
		inAddLine = 1;
		szLabel[strlen(szLabel)-1] = 0x00;
	}

	set_font("");
	display_at(1, 7, szLabel, CLR_EOL);
	if (inAddLine) vdScrollDisplay();
	write_at("0", 1, 21, 7);
	vdFlushKeyboardBuffer();
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if (karakter >= 0x30 && karakter <= 0x39) {
			if (strlen(Number)<21 && ((mode!=3) || (strlen(Number)<Max))) {
				Number[strlen(Number)] = karakter;
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberEngl(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==8) {
			if (strlen(Number)>0) {
				Number[strlen(Number)-1] = 0x00;	
				if (inAddLine) {
					display_at(1, 6, szLabel, CLR_EOL);
					display_at(1, 7, " ", CLR_EOL);
				} else {
					display_at(1, 7, szLabel, CLR_EOL);
				}
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberEngl(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==13){
			Num = atol(Number);
			if (((mode==1 || mode==2) && (Num < Min || Num > Max)) 
				|| (mode==3 && strlen(Number)<Min)){
				memset(Number, 0x00, strlen(Number));
				DisplayStatus("Out of range...!");
				vdWarnBeep();
				SVC_WAIT(1000);
				write_at("                     ", 21, 1, 7);
				if (inAddLine) {
					write_at(szLabel, strlen(szLabel), 1, 6);
				} else {
					write_at(szLabel, strlen(szLabel), 1, 7);
				}
				write_at("0", 1, 21, 7);
				DisplayStatus("");
			} else {
				*ret = Num;
				return 1;
			}
		} else if (karakter==27){
			return 0;
		} 
	} while (1);
	return 1;
}


int InputNumberWithCentInd(char *label, char *Number, long Min, long Max, int mode, long *ret)
{
	int karakter=0;
	long Num;
	char buff[40];

	set_font("");
	display_at(1, 7, label, CLR_EOL);
	write_at("0", 1, 21, 7);
	vdFlushKeyboardBuffer();
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if (karakter >= 0x30 && karakter <= 0x39) {
			if (strlen(Number)<21 && ((mode!=3) || (strlen(Number)<Max))) {
				Number[strlen(Number)] = karakter;
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberWithCentInd(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==8) {
			if (strlen(Number)>0) {
				Number[strlen(Number)-1] = 0x00;
				display_at(1, 7, label, CLR_EOL);
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberInd(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==13){
			Num = atol(Number);
			if (((mode==1 || mode==2) && (Num < Min || Num > Max)) 
				|| (mode==3 && strlen(Number)<Min)){
				memset(Number, 0x00, strlen(Number));
				DisplayStatus("Out of range...!");
				vdWarnBeep();
				SVC_WAIT(1000);
				write_at("                     ", 21, 1, 7);
				write_at(label, strlen(label), 1, 7);
				write_at("0", 1, 21, 7);
				DisplayStatus("");
			} else {
				*ret = Num;
				return 1;
			}
		} else if (karakter==27){
			return 0;
		} 
	} while (1);
	return 1;
}

int InputNumberWithCentEngl(char *label, char *Number, long Min, long Max, int mode, long *ret)
{
	int karakter=0;
	long Num;
	char buff[40];

	set_font("");
	display_at(1, 7, label, CLR_EOL);
	write_at("0", 1, 21, 7);
	vdFlushKeyboardBuffer();
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if (karakter >= 0x30 && karakter <= 0x39) {
			if (strlen(Number)<21 && ((mode!=3) || (strlen(Number)<Max))) {
				Number[strlen(Number)] = karakter;
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberWithCentEngl(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==8) {
			if (strlen(Number)>0) {
				Number[strlen(Number)-1] = 0x00;
				display_at(1, 7, label, CLR_EOL);
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberEngl(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==13){
			Num = atol(Number);
			if (((mode==1 || mode==2) && (Num < Min || Num > Max)) 
				|| (mode==3 && strlen(Number)<Min)){
				memset(Number, 0x00, strlen(Number));
				DisplayStatus("Out of range...!");
				vdWarnBeep();
				SVC_WAIT(1000);
				write_at("                     ", 21, 1, 7);
				write_at(label, strlen(label), 1, 7);
				write_at("0", 1, 21, 7);
				DisplayStatus("");
			} else {
				*ret = Num;
				return 1;
			}
		} else if (karakter==27){
			return 0;
		} 
	} while (1);
	return 1;
}

int InputPAN(char *label, char *Number, long Min, long Max, int mode, long *ret)
{
	int karakter=0;
	long Num;
	char buff[40];

	set_font("");
	write_at(label, strlen(label), 1, 7);
	write_at("0", 1, 21, 7);
	//vdFlushKeyboardBuffer();
	TimeOut(15000);
	do {
		if (KBHIT()) {
			karakter = get_char();
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if (karakter >= 0x30 && karakter <= 0x39) {
			if (strlen(Number)<21) {
				Number[strlen(Number)] = karakter;
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberEngl(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==8) {
			if (strlen(Number)>0) {
				Number[strlen(Number)-1] = 0x00;
				display_at(1, 7, label, CLR_EOL);
				if (mode==1) {
					memset(buff, 0x00, sizeof(buff));
					FormatNumberEngl(buff, Number);
					write_at(buff, strlen(buff), 22 - strlen(buff), 7);
				}else{
					write_at(Number, strlen(Number), 22-strlen(Number), 7);
				}
			}
		} else if (karakter==13){
			Num = atol(Number);
			if ((mode==1 || mode==2) && (Num < Min || Num > Max)) {
				memset(Number, 0x00, strlen(Number));
				DisplayStatus("Out of range...!");
				vdWarnBeep();
				SVC_WAIT(1000);
				write_at("                     ", 21, 1, 7);
				write_at(label, strlen(label), 1, 7);
				write_at("0", 1, 21, 7);
				DisplayStatus("");
			} else {
				*ret = Num;
				return 1;
			}
		} else if (karakter==27){
			return 0;
		} 
	} while (1);
	return 1;
}

int FormatNumInd(char *ResBuff, long Number)
{
	char buff[40];
	sprintf(buff, "%ld", Number);
	return FormatNumberInd(ResBuff, buff);
}

int FormatNumEngl(char *ResBuff, long Number)
{
	char buff[40];
	sprintf(buff, "%ld", Number);
	return FormatNumberEngl(ResBuff, buff);
}


int FormatNumWithCentInd(char *ResBuff, long Number)
{
	char buff[40];
	sprintf(buff, "%ld", Number);
	return FormatNumberWithCentInd(ResBuff, buff);
}

int FormatNumWithCentEngl(char *ResBuff, long Number)
{
	char buff[40];
	sprintf(buff, "%ld", Number);
	return FormatNumberWithCentEngl(ResBuff, buff);
}

int FormatNumberInd(char *ResBuff, char *InpBuff)
{
	char buff[40];
	int i=0;
	int found = 0;

	strcpy(buff, InpBuff);
	for (i=0; buff[i]!=0x00; i++) {
		if (buff[i] != '0') {
			strcpy(buff, &buff[i]);
			break;
		} else {
			found=1;
		}
	}
	if (found==1 && strlen(buff)==strlen(InpBuff)) strcpy(buff, "0");

	if (strlen(buff)>3) {
		strcpy(ResBuff, &buff[strlen(buff)-3]);
		buff[strlen(buff)-3] = 0x00;
	} else {
		strcpy(ResBuff, buff);
		buff[0] = 0x00;
	}

	while (strlen(buff)>0){
		if (strlen(buff)>3) {
			memmove(&ResBuff[4], ResBuff, strlen(ResBuff));
			ResBuff[3] = '.';
			strncpy(ResBuff, &buff[strlen(buff)-3], 3);
			buff[strlen(buff)-3] = 0x00;
		} else {
			memmove(&ResBuff[strlen(buff)+1], ResBuff, strlen(ResBuff));
			ResBuff[strlen(buff)] = '.';
			strncpy(ResBuff, buff, strlen(buff));
			break;
		}
	}
	return 1;
}

int FormatNumberEngl(char *ResBuff, char *InpBuff)
{
	char buff[40];
	int i=0;
	int found = 0;

	strcpy(buff, InpBuff);
	for (i=0; buff[i]!=0x00; i++) {
		if (buff[i] != '0') {
			strcpy(buff, &buff[i]);
			break;
		} else {
			found=1;
		}
	}
	if (found==1 && strlen(buff)==strlen(InpBuff)) strcpy(buff, "0");

	if (strlen(buff)>3) {
		strcpy(ResBuff, &buff[strlen(buff)-3]);
		buff[strlen(buff)-3] = 0x00;
	} else {
		strcpy(ResBuff, buff);
		buff[0] = 0x00;
	}

	while (strlen(buff)>0){
		if (strlen(buff)>3) {
			memmove(&ResBuff[4], ResBuff, strlen(ResBuff));
			ResBuff[3] = ',';
			strncpy(ResBuff, &buff[strlen(buff)-3], 3);
			buff[strlen(buff)-3] = 0x00;
		} else {
			memmove(&ResBuff[strlen(buff)+1], ResBuff, strlen(ResBuff));
			ResBuff[strlen(buff)] = ',';
			strncpy(ResBuff, buff, strlen(buff));
			break;
		}
	}
	return 1;
}

int FormatNumberWithCentInd(char *ResBuff, char *InpBuff)
{
	char buff[40];
	char buff_res[40];
	char Cent[10];

	memset(Cent, 0x00, sizeof(Cent));

	strcpy(buff, InpBuff);
	if (strlen(InpBuff)>=2) {
		buff[strlen(InpBuff)-2] = 0x00;
		strcpy(Cent, &InpBuff[strlen(InpBuff)-2]);
	} else if (strlen(InpBuff)==1){
		buff[0]=0x30;
		Cent[0]=0x30;
		Cent[1]=InpBuff[0];
	} else {
		buff[0]=0x30;
		Cent[0]=0x30;
		Cent[1]=0x30;
	}
	memset(buff_res, 0x00, sizeof(buff_res));
	FormatNumberInd(buff_res, buff);
	sprintf(ResBuff, "%s,%s", buff_res, Cent);

	return 1;
}

int FormatNumberWithCentEngl(char *ResBuff, char *InpBuff)
{
	char buff[40];
	char buff_res[40];
	char Cent[10];

	memset(Cent, 0x00, sizeof(Cent));

	strcpy(buff, InpBuff);
	if (strlen(InpBuff)>=2) {
		buff[strlen(InpBuff)-2] = 0x00;
		strcpy(Cent, &InpBuff[strlen(InpBuff)-2]);
	} else if (strlen(InpBuff)==1){
		buff[0]=0x30;
		Cent[0]=0x30;
		Cent[1]=InpBuff[0];
	} else {
		buff[0]=0x30;
		Cent[0]=0x30;
		Cent[1]=0x30;
	}
	memset(buff_res, 0x00, sizeof(buff_res));
	FormatNumberEngl(buff_res, buff);
	sprintf(ResBuff, "%s.%s", buff_res, Cent);

	return 1;
}

void GetTopDateTime(char *Dest) 
{
	char TimeBuff[20];
	memset(TimeBuff, 0x00, sizeof(TimeBuff));
	SVC_CLOCK(0, TimeBuff, 15);
		
	Dest[0] = TimeBuff[6];
	Dest[1] = TimeBuff[7];
	Dest[2] = '/';
	Dest[3] = TimeBuff[4];
	Dest[4] = TimeBuff[5];
	Dest[5] = '/';
	Dest[6] = TimeBuff[2];
	Dest[7] = TimeBuff[3];
	Dest[8] = 0x20;
	Dest[9] = 0x20;
	Dest[10] = 0x20;
	Dest[11] = TimeBuff[8];
	Dest[12] = TimeBuff[9];
	Dest[13] = ':';
	Dest[14] = TimeBuff[10];
	Dest[15] = TimeBuff[11];
	Dest[16] = 0x00;
}

void FormatDate(char *Dest, char *Date)
{
	char TimeBuff[20];
	memset(TimeBuff, 0x00, sizeof(TimeBuff));
	SVC_CLOCK(0, TimeBuff, 15);
	if (strlen(Date)==8) {
		Dest[0] = Date[6];
		Dest[1] = Date[7];
		Dest[2] = '/';
		Dest[3] = Date[4];
		Dest[4] = Date[5];
		Dest[5] = '/';
		Dest[6] = Date[0];
		Dest[7] = Date[1];
		Dest[8] = Date[2];
		Dest[9] = Date[3];
		Dest[10] = 0x00;
	} else if (strlen(Date)==6) {
		Dest[0] = Date[4];
		Dest[1] = Date[5];
		Dest[2] = '/';
		Dest[3] = Date[2];
		Dest[4] = Date[3];
		Dest[5] = '/';
		Dest[6] = TimeBuff[0];
		Dest[7] = TimeBuff[1];
		Dest[8] = Date[0];
		Dest[9] = Date[1];
		Dest[10] = 0x00;
	} else if (strlen(Date)==4){
		Dest[0] = Date[2];
		Dest[1] = Date[3];
		Dest[2] = '/';
		Dest[3] = Date[0];
		Dest[4] = Date[1];
		Dest[5] = 0x00;
	} else {
		Dest[0] = TimeBuff[6];
		Dest[1] = TimeBuff[7];
		Dest[2] = '/';
		Dest[3] = TimeBuff[4];
		Dest[4] = TimeBuff[5];
		Dest[5] = '/';
		Dest[6] = TimeBuff[0];
		Dest[7] = TimeBuff[1];
		Dest[8] = TimeBuff[2];
		Dest[9] = TimeBuff[3];
		Dest[10] = 0x00;
	}
}

void FormatTime(char *Dest, char *Time)
{
	char TimeBuff[20];
	memset(TimeBuff, 0x00, sizeof(TimeBuff));
	SVC_CLOCK(0, TimeBuff, 15);
	if (strlen(Time)==6) {
		Dest[0] = Time[0];
		Dest[1] = Time[1];
		Dest[2] = ':';
		Dest[3] = Time[2];
		Dest[4] = Time[3];
		Dest[5] = ':';
		Dest[6] = Time[4];
		Dest[7] = Time[5];
		Dest[8] = 0x00;
	} else {
		Dest[0] = TimeBuff[8];
		Dest[1] = TimeBuff[9];
		Dest[2] = ':';
		Dest[3] = TimeBuff[10];
		Dest[4] = TimeBuff[11];
		Dest[5] = ':';
		Dest[6] = TimeBuff[12];
		Dest[7] = TimeBuff[13];
		Dest[8] = 0x00;
	}
}

void vdGetMonthStr(char *buff, int inMonth)
{
	switch (inMonth) {
		case 1:
			strcpy(buff, "JANUARI");
			break;
		case 2:
			strcpy(buff, "FEBRUARI");
			break;
		case 3:
			strcpy(buff, "MARET");
			break;
		case 4:
			strcpy(buff, "APRIL");
			break;
		case 5:
			strcpy(buff, "MEI");
			break;
		case 6:
			strcpy(buff, "JUNI");
			break;
		case 7:
			strcpy(buff, "JULI");
			break;
		case 8:
			strcpy(buff, "AGUSTUS");
			break;
		case 9:
			strcpy(buff, "SEPTEMBER");
			break;
		case 10:
			strcpy(buff, "OKTOBER");
			break;
		case 11:
			strcpy(buff, "NOVEMBER");
			break;
		case 12:
			strcpy(buff, "DESEMBER");
			break;
		default:
			strcpy(buff, "");
			break;
	}
}

void vdFormatText(char *buff, int kolom, int Justify)
{
  char buffer[128];
  char buffspace[128];
  memset(buffer, 0x00, sizeof(buffer));
  memset(buffspace, 0x20, sizeof(buffspace));

  if (strlen(buff)>kolom) buff[kolom] = 0x00;
  if (Justify==CENTERED) {
	  strncpy(buffer, buffspace, (kolom-strlen(buff))/2);
	  strcat(buffer, buff);
	  strncat(buffer, buffspace, (kolom-strlen(buff))/2);
  } else if (Justify==R_TEXT) {
	  strncpy(buffer, buffspace, (kolom-strlen(buff)));
	  strcat(buffer, buff);
  }
  strcpy(buff, buffer);
  buff[kolom] = 0x00;
}

void clrscrplus(char *msg)
{
	static char msgbuff[22] = {'0x00'};

	if (strlen(msg)>21) {
		strncpy(msgbuff, msg, 21);
		msg[21] = 0x00;
	} else if (strlen(msg)){
		strcpy(msgbuff, msg);
	}
	ClearSevenLine();
	if (strlen(msgbuff)) DisplayJudul(msgbuff);
}

void ClearSevenLine(void) 
{
	int i;
	set_font("");
	for (i=0; i<7; i++) {
		gotoxy(1, 2);
		delline();
	}
}

void vdClearScreen(void)
{
	char hrf[20];
	memset(hrf, 0x00, sizeof(hrf));
	get_font(hrf);
	set_font("F:asc4x16.vft");
	write_at("                             ", 16, 1, 1);
	write_at("                             ", 16, 1, 2);
	write_at("                             ", 16, 1, 3);
	write_at("                             ", 16, 1, 4);
	set_font(hrf);
}

void vdScrollDisplay(void)
{
	set_font("");
	gotoxy(1, 2);
	delline();
	gotoxy(1,8);
}

void vdFlushKeyboardBuffer()
{
    KBHIT();        /* Don't ask why just trust that a lot of */
                    /* time was wasted tracking this down     */
	if (get_console(0) >= 0)
	{
		while (KBHIT())
		get_char();
	}
}

void vdFlushCardBuffer(void)
{
	char	stDummyBuf[CARD_SIZE + 1];

	while ( read(hMagReader, stDummyBuf, CARD_SIZE) > 0 ) ;
}

void vdWarnBeep(void)
{
    int inCount;
	LOG_PRINTFF((0x08L, "BEEP...BEEP...BEEP"));
    for (inCount = 0; inCount < 3; inCount++){
        vdBeep();
    }
}

void vdBeep(void)
{    
    vdDelay(BEEP_DELAY);
    error_tone();
}

void vdDelay (int inMilliSec)
{
	SVC_WAIT(inMilliSec);
	return;
}

int TimeOut(unsigned long TheTime)
{
	static unsigned long timeout;
	static unsigned long ulAddition = 0L;
	
	if (TheTime!=0) {
		timeout = read_ticks() + TheTime;
		ulAddition = 0L;
		return 1;
	} else {
		unsigned long ulCurrTicks = read_ticks(); 
		if (ulCurrTicks>timeout) {
			if (ulAddition==0L) {
				vdWarnBeep();
				ulAddition = 3000L;
				timeout += 3000L;
				return 0;
			}
			return 1;
		} else {
			return 0;
		}
	}
	return 0;
}

int inPvsTimeOut(unsigned long TheTime)
{
	static unsigned long ulPvsTimeout;
	
	if (TheTime) {
		ulPvsTimeout = read_ticks() + TheTime;
		return 1;
	} else {
		unsigned long ulCurrTicks = read_ticks(); 
		if (ulCurrTicks>ulPvsTimeout) {
			return 1;
		} else {
			return 0;
		}
	}
	return 0;
}


int InputString(char *Caption, char *Buffer)
{
   char buff[128];
   memset(buff, 0x00, sizeof(buff));
   set_font("");
   write_at(Caption, strlen(Caption), 1, 7);
   window(strlen(Caption)+1, 7, 21, 7);
   gotoxy(1, 1); write_at("_", 1, 1, 1);
#ifdef __arm
   if (SVC_KEY_TXT(buff, 1, 16, 1, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#elif _TARG_68000
   if (SVC_KEY_TXT(buff, 1, 16, 1, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#endif
	   window(1, 1, 21, 8);
	   strcpy(Buffer, "");
	   return -1;
   }
   window(1, 1, 21, 8);
   buff[buff[0]]=0x00;
   buff[21] = 0x00;
   strcpy(Buffer, &buff[1]);
   //write_at("                         ", 21, 1, 7);
   //write_at(Caption, strlen(Caption), 1, 7);
   //write_at(Buffer, strlen(Buffer), 22-strlen(Buffer), 7);
   return 1;
}

int inNewInputString(char *label, char *Number, long Min, long Max)
{
	int karakter=0;
	long Num;
	char buff[40];
	int inAddLine = 0;
	char szLabel[30];

	memset(szLabel, 0x00, sizeof(szLabel));
	strcpy(szLabel, label);
	if (strlen(szLabel)>1 && szLabel[strlen(szLabel)-1] == '-') {
		inAddLine = 1;
		szLabel[strlen(szLabel)-1] = 0x00;
	}

	set_font("");
	display_at(1, 7, szLabel, CLR_EOL);
	if (inAddLine) vdScrollDisplay();
	write_at("_", 1, 21, 7);
	vdFlushKeyboardBuffer();
	TimeOut(15000);
	do {
		if (KBHIT()) {
			//char buff[20];
			karakter = get_char();
			//sprintf(buff, "%d", karakter);
			//DisplayStatus(buff);
			TimeOut(15000);
		} else {
			if (TimeOut(0)) return 0;
			SVC_WAIT(0);
			continue;
		}
		if ((karakter >= 0x30 && karakter <= 0x39) || (karakter=='*' || karakter=='#')) {
			if ((strlen(Number)<21) && (strlen(Number)<Max)) {
				Number[strlen(Number)] = karakter;
				write_at(Number, strlen(Number), 22-strlen(Number), 7);
			}
		} else if (karakter==15 && strlen(Number)>0) {
			int i=0;
			int j=0;
			int found=0;
			for (i=0; i<MAX_ALPNUM_KEYS; i++) {
				for (j=0; j<CHAR_PER_KEY; j++) {
					if (szKeyMap[i][j] == Number[strlen(Number)-1]) {
						j++;
						if (j==4) j=0;
						Number[strlen(Number)-1] = szKeyMap[i][j];
						found=1;
						break;
					}
				}
				if (found) break;
			}
			write_at(Number, strlen(Number), 22-strlen(Number), 7);
		} else if (karakter==8) {
			if (strlen(Number)>0) {
				Number[strlen(Number)-1] = 0x00;	
				if (inAddLine) {
					display_at(1, 6, szLabel, CLR_EOL);
					display_at(1, 7, " ", CLR_EOL);
				} else {
					display_at(1, 7, szLabel, CLR_EOL);
				}
				write_at(Number, strlen(Number), 22-strlen(Number), 7);
			} else {
				write_at("_", 1, 21, 7);
			}
		} else if (karakter==13){
			if (strlen(Number)<Min){
				memset(Number, 0x00, strlen(Number));
				DisplayStatus("Out of range...!");
				vdWarnBeep();
				SVC_WAIT(1000);
				write_at("                     ", 21, 1, 7);
				if (inAddLine) {
					write_at(szLabel, strlen(szLabel), 1, 6);
				} else {
					write_at(szLabel, strlen(szLabel), 1, 7);
				}
				write_at("_", 1, 21, 7);
				DisplayStatus("");
			} else {
				return 1;
			}
		} else if (karakter==27){
			return 0;
		} 
	} while (1);
	return 1;
}

int GetAndVerifyPassword(void)
{
   char buff[10];
   char buff2[128];
   memset(buff2, 0x00, sizeof(buff2));
   memset(buff, 0x00, sizeof(buff));
   set_font("");
   write_at("Password : ", 11, 1, 7);
   window(12, 7, 21, 8);
   gotoxy(1, 1);
#ifdef __arm
   if (SVC_KEY_TXT(buff, 2, 6, 4, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#elif _TARG_68000
   if (SVC_KEY_TXT(buff, 2, 6, 4, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#endif
	   window(1, 1, 21, 8);
	   vdClearScreen();
	   return 0;
   }
   window(1, 1, 21, 8);
   vdClearScreen();
   buff[buff[0]]=0x00;
   get_env_x(PWD_ADMIN, 0, buff2);
   if (strlen(buff2)) {
      if (strcmp(&buff[1], buff2)) return 0;
	  else return 1;
   } else {
      return 0;
   }
   return 0;
}

int ChangePassword(void)
{
   char buff[10];
   char buff2[128];
   memset(buff2, 0x00, sizeof(buff2));
   memset(buff, 0x00, sizeof(buff));
   set_font("");
   write_at("New Password : ", 15, 1, 7);
   window(16, 7, 21, 8);
   gotoxy(1, 1);
#ifdef __arm 
   if (SVC_KEY_TXT(buff, 2, 6, 4, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#elif _TARG_68000
   if (SVC_KEY_TXT(buff, 2, 6, 4, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#endif
	   window(1, 1, 21, 8);
	   return 0;
   }
   window(1, 1, 21, 8);
   buff[buff[0]]=0x00;
   strcpy(buff2, buff);
   vdScrollDisplay();
   memset(buff, 0x00, sizeof(buff));
   display_at(1, 7, "Confirmation : ", CLR_EOL);
   window(16, 7, 21, 8);
   gotoxy(1, 1);
#ifdef __arm
   if (SVC_KEY_TXT(buff, 2, 6, 4, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#elif _TARG_68000
   if (SVC_KEY_TXT(buff, 2, 6, 4, (char *)szKeyMap, sizeof(szKeyMap)) < 1) {
#endif
	   window(1, 1, 21, 8);
	   return 0;
   }
   window(1, 1, 21, 8);
   buff[buff[0]]=0x00;
   if (strcmp(buff2, buff)) {
      DisplayStatus("Konfirmasi Fail!");
	  SVC_WAIT(2000);
	  return 0;
   }
   if (put_env_x(PWD_ADMIN, 0, &buff2[1]) != -1) {
      DisplayStatus("Password Changed");
	  SVC_WAIT(1000);
	  return 1;
   } else {
      DisplayStatus("Fail!");
	  SVC_WAIT(1000);
	  return 0;
   }
   return 0;
}

unsigned long ulGetAndIncInvNum(void)
{
	char buffer[128];
	unsigned long InvNum = 0;

	memset(buffer, 0x00, sizeof(buffer));
	get_env_x("INVNUM", 0, buffer);

	InvNum = atol(buffer);
	if (InvNum) {
		memset(buffer, 0x00, sizeof(buffer));
		if (InvNum==999999L) {
			sprintf(buffer, "%ld", 0L);
		} else {
			sprintf(buffer, "%ld", InvNum+1);
		}
		put_env_x("INVNUM", 0, buffer);
		return (InvNum);
	}
	return 0;
}

int inGetAutoReversalTimer(void)
{
	char buffer[128];
	int inAutoR = 0;

	memset(buffer, 0x00, sizeof(buffer));
	get_env_x(TRX_AUTO_REVERSAL, 0, buffer);

	inAutoR = atoi(buffer);
	if (inAutoR<=0) inAutoR = 10;

	return inAutoR;
}

int inEnablePrintISOTrace(void)
{
   char buff[128];
   memset(buff, 0x00, sizeof(buff));
   get_env_x("ISO-TRACE", 0, buff);
   if (buff[0]=='1') return 1;
   return 0;
}

int inGetPrintTimes(void)
{
	char buffer[128];
	int inPrintTimes = 0;

	memset(buffer, 0x00, sizeof(buffer));
	get_env_x(PRT_COPY, 0, buffer);

	inPrintTimes = atoi(buffer);
	if (inPrintTimes<=0) inPrintTimes = 1;

	return inPrintTimes;
}


void vdTrimSpace(char *buff)
{
	int i=0;

	for (i=0; buff[i]!=0x00; i++) {
		if (!isspace(buff[i])) {
			strcpy(buff, &buff[i]);
			break;
		}
	}
	while (strlen(buff)) {  //trimspace 
		if (isspace(buff[strlen(buff)-1])) {
			buff[strlen(buff)-1] = 0x00;
		} else {
			break;
		}
	}
}

void vdGetCurrency(char *szCurr, char *szInput)
{
	char buff[5];
	memset(buff, 0x00, sizeof(buff));
	strncpy(buff, szInput, 3);

	if (!strcmp(buff, "840")) {
		strcpy(szCurr, "US$");
	} else if (!strcmp(buff, "360")) {
		strcpy(szCurr, "RP.");
	} else if (!strcmp(buff, "IDR")) {
		strcpy(szCurr, "RP.");
	} else if (!strcmp(buff, "idr")) {
		strcpy(szCurr, "RP.");
	} else {
		strcpy(szCurr, buff);
	}
}

void vdMaskNameString(char *buff)
{
	int i=0;
	int inNotSpaceCount = 0;
	if (strlen(buff)==0) return;
	for (i=0; i<strlen(buff); i++) {
		if (!isspace(buff[i])) {
			inNotSpaceCount++;
			if (inNotSpaceCount>=3) {
				buff[i] = '*';
			}
		} else {
			inNotSpaceCount = 0;
		}
	}
}

int inUseSNasTID(void)
{
   char buff[128];
   memset(buff, 0x00, sizeof(buff));
   get_env_x("SN-AS-TID", 0, buff);
   if (atoi(buff)==1) return 1;
   return 0;
}

int inPvsDisplayBatteryLevel(int inBatteryLevel)
{
	char hrf[20];
	char buff[30];

	memset(hrf, 0x00, sizeof(hrf));
	memset(buff, 0x00, sizeof(buff));

	get_font(hrf);
	set_font("F:WifiLogo.vft");

	buff[0]= 12 + inBatteryLevel/10;

	write_at(buff, 1, 1, 2);
	set_font(hrf);
}

int inDisplayBatteryLevel(void)
{
	static unsigned long ticks = 0L;
	unsigned long curr_ticks = 0L;
	int inBatteryLevel = 0;
	int inBatteryStatus = 0;
	static int inChargingFactor = 0L;
	
	curr_ticks = read_ticks();
	if (curr_ticks < ticks || curr_ticks>ticks + 1000L){
	} else {
		return VS_SUCCESS;
	}

	inBatteryStatus = get_battery_status();
	if (get_dock_status()==HANDHELD_UNDOCKED){
		inBatteryLevel = get_battery_charge();
		inPvsDisplayBatteryLevel(inBatteryLevel);
	} else {
		inBatteryLevel = get_battery_charge();
		if ((inBatteryLevel>=0) && (inBatteryLevel<99)) {
			inBatteryLevel = inBatteryLevel + inChargingFactor;
			if (inBatteryLevel>=100) {
				if (inChargingFactor > 0) {
					inChargingFactor = 0;
					inPvsDisplayBatteryLevel(inBatteryLevel);
				}
			} else {
				inChargingFactor += 10;
				inPvsDisplayBatteryLevel(inBatteryLevel);
			}								
		}
		//if (inPvsUnlockKeyboard==0){
		//	vdPvsUnlockKeyboardAndDisplayOn(1);
		//}
	}
	ticks = curr_ticks;
	return VS_SUCCESS;
}